package com.gmail.dengtao.joe.group.transport.impl;

import com.gmail.dengtao.joe.group.channel.Channel;
import com.gmail.dengtao.joe.group.interceptor.InterceptorStack;
import com.gmail.dengtao.joe.group.transport.ChannelReceiver;

/**
 * Abstract Channel Reveiver.
 * @author <a href="mailto:joe.dengtao@gmail.com">DengTao</a>
 * @version 1.0
 * @since 1.0
 */
abstract class AbstractChannelReceiver implements ChannelReceiver {

	public static final int DEFAULT_BUFFER_SIZE = 512;
	
	protected Channel channel;
	protected InterceptorStack interceptorStack = null;
	
	protected String host;					// listen hostname.
	protected int port;						// listen port.
	protected int idleTimeForRead = 0;		// idle time for read in seconds.
	protected int receiveBufferSize = DEFAULT_BUFFER_SIZE; // use 538 ~ 548, recommend 538 for the best performance.

	protected int selectTimeout = 100;			// If positive, block for up to timeout milliseconds, more or less, while waiting for a channel to become ready; if zero, block indefinitely; must not be negative

	protected boolean start = false;			// Mark whether receiver is started.

	@Override
	public int getIdleTimeForRead() {
		return idleTimeForRead;
	}

	@Override
	public String getHost() {
		return host;
	}

	@Override
	public int getPort() {
		return port;
	}

	@Override
	public void setIdleTimeForRead(int idleTimeForRead) {
		if (idleTimeForRead < 0) {
			throw new IllegalArgumentException("Illegal idle time for read: " + idleTimeForRead);
		}
		this.idleTimeForRead = idleTimeForRead;
	}

	@Override
	public int getSelectTimeout() {
		return selectTimeout;
	}

	@Override
	public void setSelectTimeout(int selectTimeout) {
		if (selectTimeout < 0) {
			throw new IllegalArgumentException("Negative timeout:" + selectTimeout);
		}
		this.selectTimeout = selectTimeout;
	}
	
	@Override
    public int getReceiveBufferSize() {
		return receiveBufferSize;
	}
	
	@Override
	public void setReceiveBufferSize(int receiveBufferSize) throws IllegalArgumentException  {
		if (receiveBufferSize < 0) {
			throw new IllegalArgumentException("Illegal size for receive buffer: " + receiveBufferSize);
		}
		this.receiveBufferSize = receiveBufferSize;
	}

	@Override
	public InterceptorStack getInterceptorStack() {
		return interceptorStack;
	}

	@Override
	public void setInterceptorStack(InterceptorStack interceptorStack) {
		this.interceptorStack = interceptorStack;
	}

}
